#ifndef GLPANEL_H
#define GLPANEL_H

#include <qgl.h>
#include <vector>
#include <map>
#include <QPainter>
using namespace std;
class QMouseEvent;
#include "../hed/hedcontroller.h"
#include "../model/model.h"
#include "edumygui.h"
#include "ui_edumygui.h"


class GLPanel : public QGLWidget
{
    Q_OBJECT

public:

    GLPanel( QWidget* parent, const char* name );
    ~GLPanel();
  	void setMyUI( EduMyGUI* _ui );
    void setUserOperation( int _oper, Qt::CursorShape _cursor );
    void setTopolViewFlag( int _type, bool _isVisible );
    void setGridSnapData( bool _isVisible, bool _isSnapOn, double _dx, double _dy );
    bool getGridSnapInfo( double& _dx, double& _dy );
    bool hasUndoing();
    bool hasRedoing();
    Model* getModel();
//*********************************************
	void selectRegion(vector<int> _numFace);
	void verifyAndAddFace();
  void setArea(double& _area, int _numFace);
  void setCentroide( double& _xc,double& _yc, int _numFace );
  void getMomentOfInertia( double& _Ixx, double& _Iyy, double& _Ixy, int _numFace );
  HEDController* getHedController();


public slots:

    void mousePressEvent( QMouseEvent* event );
    void mouseMoveEvent( QMouseEvent* event );
    void mouseReleaseEvent(QMouseEvent *event);
    void wheelEvent(QWheelEvent *event);
	void keyPressEvent ( QKeyEvent * event );
	void keyReleaseEvent ( QKeyEvent * event );

protected:

    void		initializeGL();
    void		paintGL();
    void		resizeGL( int w, int h );
    virtual GLuint 	makeObject();
    virtual GLuint 	drawLine();
    virtual GLuint 	drawCursorTarget();
    void tri( GLenum primitive, GLdouble x1, GLdouble y1, GLdouble z1,
              GLdouble x2, GLdouble y2, GLdouble z2,
              GLdouble x3, GLdouble y3, GLdouble z3);
    void quad( GLenum primitive, GLdouble x1, GLdouble y1, GLdouble z1,
               GLdouble x2, GLdouble y2, GLdouble z2,
               GLdouble x3, GLdouble y3, GLdouble z3,
               GLdouble x4, GLdouble y4, GLdouble z4);
    void fitUniverseToViewport();
    QPointF convertPtCoordsToUniverse( QPoint _pt );
    void viewGrid();
    void snapToGrid( QPointF& _pt );


private:

    GLuint m_objModel;
    Model* m_model;
	//QPainter painter;
	QPointF distPointBegin;
    QPointF distPointEnd;
	// The QPoint is a Qt class that defines a point in a plane
    // i.e. the position of the mouse on the screen.
    int m_raster_x;
    int m_raster_y;
    int m_old_raster_x;
    int m_old_raster_y;
    GLfloat m_scaling;

    // coordenadas no Universo dos vertices da curva coletada
    double m_pickX0;
    double m_pickY0;
    double m_pickX;
    double m_pickY;
    double m_mouseX;
    double m_mouseY;
    double m_mouseTol;
    bool m_isCollectingGeom;
	  int m_keytype;

    // tamanho da janela
    int m_w;
    int m_h;

    // coordenadas Modelo
    double m_minXM;
    double m_maxXM;
    double m_minYM;
    double m_maxYM;
    // coordenadas Universo
    double m_minXU;
    double m_maxXU;
    double m_minYU;
    double m_maxYU;

    // 0:Select,1:Move,2:Zoom,3:Point,4:Line,5:Rectangle,6:Undo,7:Redo
    int m_currUserOper;
    Qt::CursorShape m_currCursor;

    HEDController m_controller;

    // visualization flag for topological entities
    bool m_viewVertex;
    bool m_viewEdge;
    bool m_viewFace;
    bool m_viewAttrib;
    bool m_viewGrid;
    bool m_isSnapOn;
    double m_gridX;
    double m_gridY;

	//*****************************
	bool changeFace;
	int m_numFace;

    // to access undo redo buttons
	EduMyGUI* edum_ui;

    QColor m_colorBackground;
    QColor m_colorCursorTarget;
    QColor m_colorCollecting;
    QColor m_colorSelection;
    QColor m_colorVertex;
    QColor m_colorEdges;
    QColor m_colorFaces;
    QColor m_colorMeshE;
    QColor m_colorMeshF;
    QColor m_colorAxis;
    QColor m_colorGrid;
    QColor m_colorAttrib;
};


#endif // GLPANEL_H
